import { ArrayStack } from "./01_数组实现栈结构";

let s = "{[]}"; //true
let s1 = "()[]{}"; //true
let s2 = "(]"; //false

const isFlag = (str: string): boolean => {
  const arrayStack = new ArrayStack<string>();
  const strArr = str.split("");
  console.log(strArr);
  const symbolArr = ["{", "[", "("];
  for (let i = 0; i < strArr.length; i++) {
    const strSymbol = strArr[i];
    if (symbolArr.includes(strSymbol)) {
      arrayStack.push(strSymbol);
    } else {
      if (strSymbol === "}" && arrayStack.peek() === "{") {
        arrayStack.pop();
      } else if (strSymbol === "]" && arrayStack.peek() === "[") {
        arrayStack.pop();
      } else if (strSymbol === ")" && arrayStack.peek() === "(") {
        arrayStack.pop();
      }
    }
  }
  return arrayStack.isEmpty();
};
// console.log(isFlag(s));
// console.log(isFlag(s1));
// console.log(isFlag(s2));

const isValid = function (s: string): boolean {
  const stack = new ArrayStack<string>();

  for (let i = 0; i < s.length; i++) {
    const c = s[i];
    switch (c) {
      case "(":
        stack.push(")");

        break;
      case "{":
        stack.push("}");

        break;
      case "[":
        stack.push("]");

        break;

      default:
        if (c !== stack.pop()) return false;
        break;
    }
  }
  return stack.isEmpty();
};

console.log(isValid(s));
console.log(isValid(s1));
console.log(isValid(s2));
